package mfuny.ggongal.admin.etc;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import mfuny.ggongal.admin.debug.Debug;

public class EmailSender {
	
	//mfunygame@gmail.com , 엠퍼니1004
	public void sendMail(String from, String to, String subject, String content) throws Exception {
		Properties p = new Properties();
		p.put("mail.smtp.user", "mfunygame");
		p.put("mail.smtp.host", "smtp.gmail.com");
		p.put("mail.smtp.port", "587");
		p.put("mail.smtp.starttls.enable", "true");
		p.put("mail.smtp.auth", "true");
		p.put("mail.smtp.debug", "true");
		p.put("mail.smtp.socketFactory.port", "465");
		p.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		p.put("mail.smtp.socketFactory.fallback", "false");
		String[] to2 = to.split(",");
		
		try {
			Authenticator auth = new SMTPAuthenticator();
			Session session = Session.getInstance(p, auth);
			MimeMessage msg = new MimeMessage(session);
			msg.setSubject(subject);
			msg.setFrom(new InternetAddress(from));
			InternetAddress[] add = new InternetAddress[to2.length];
			for(int i = 0; i < add.length; i++) {
				add[i] = new InternetAddress(to2[i]);
			}
			msg.addRecipients(Message.RecipientType.TO, add);
			msg.setContent(content, "text/html;charset=UTF-8");
			Transport.send(msg);
			
		} catch(Exception e) {
			Debug.toDebugFile(e, "sendMail");
		}
	}
}
